package cookie.swing;

import cookie.Main;
import cookie.wrappers.Input;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.awt.*;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.swing.GroupLayout;
import javax.swing.LayoutStyle;

/**
 * Created by IntelliJ IDEA.
 * User: Nathaniel
 * Date: 12/10/11
 * Time: 3:30 PM
 */

public class MakeGUI extends JFrame {
    private ArrayList<Input> questions = new ArrayList<Input>();

    private String name;
    
    private File file;

    public MakeGUI() {
        while(name == null || name.length() <= 0){
            name = JOptionPane.showInputDialog(null, "Enter list name.", "Apex Homework", JOptionPane.PLAIN_MESSAGE);
        }
        file = new File(System.getenv("USERPROFILE") + File.separator + "Apex" + File.separator + "Homework" + File.separator + "Lists" + File.separator + name + ".xml");
        if(file.exists()){
            boolean rename = JOptionPane.showConfirmDialog(null, "That list already exists.\nDo you wish to overwrite this list?", "Warning", JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION;
            if(rename){
                while(name == null || name.length() <= 0){
                    name = JOptionPane.showInputDialog(null, "Enter list name.", "Apex Homework", JOptionPane.PLAIN_MESSAGE);
                }
                file = new File(System.getenv("USERPROFILE") + File.separator + "Apex" + File.separator + "Homework" + File.separator + "Lists" + File.separator + name + ".xml");
            }
        }
        
        questionField = new JTextField();
        answerField = new JTextField();
        questionLabel = new JLabel();
        answerLabel = new JLabel();
        addBtn = new JButton();
        clearBtn = new JButton();
        finishBtn = new JButton();

        setTitle("Apex Homework - Make new list");
        Container contentPane = getContentPane();

        questionLabel.setText("Question:");

        answerLabel.setText("Answer:");

        addBtn.setText("Add");
        addBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    if(questionField.getText().length() <= 0 || answerField.getText().length() <= 0) {
                        JOptionPane.showMessageDialog(null, "Please fill in both textfields.", "Error", JOptionPane.PLAIN_MESSAGE);
                        return;
                    }
                    questions.add(new Input(questionField.getText(), answerField.getText()));
                    questionField.setText("");
                    answerField.setText("");
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        });

        clearBtn.setText("Clear list");
        clearBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    questions.clear();
                    questionField.setText("");
                    answerField.setText("");
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        });

        finishBtn.setText("Finish");
        finishBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    StringBuilder builder = new StringBuilder();
                    builder.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
                    builder.append("<list>\n");
                    for(Input i : questions){
                        builder.append("\t<input>\n");
                        builder.append("\t\t<question>" + i.getQuestion() + "</question>\n");
                        builder.append("\t\t<answer>" + i.getAnswer() + "</answer>\n");
                        builder.append("\t</input>\n");
                    }
                    builder.append("</list>");
                    if(!file.exists()){
                        file.createNewFile();
                    }
                    file.setWritable(true, false);
                    BufferedWriter out = new BufferedWriter(new FileWriter(file));
                    out.write(builder.toString());
                    out.close();
                    Main m = new Main();
                    m.setVisible(true);
                    dispose();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        });

        GroupLayout contentPaneLayout = new GroupLayout(contentPane);
        contentPane.setLayout(contentPaneLayout);
        contentPaneLayout.setHorizontalGroup(
                contentPaneLayout.createParallelGroup()
                        .addGroup(contentPaneLayout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(contentPaneLayout.createParallelGroup()
                                        .addComponent(answerField, GroupLayout.DEFAULT_SIZE, 284, Short.MAX_VALUE)
                                        .addComponent(questionLabel)
                                        .addComponent(answerLabel)
                                        .addComponent(questionField, GroupLayout.DEFAULT_SIZE, 284, Short.MAX_VALUE)
                                        .addGroup(contentPaneLayout.createSequentialGroup()
                                                .addComponent(addBtn, GroupLayout.PREFERRED_SIZE, 88, GroupLayout.PREFERRED_SIZE)
                                                .addGap(5, 5, 5)
                                                .addComponent(clearBtn, GroupLayout.PREFERRED_SIZE, 97, GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(finishBtn, GroupLayout.DEFAULT_SIZE, 88, Short.MAX_VALUE)))
                                .addContainerGap())
        );
        contentPaneLayout.setVerticalGroup(
                contentPaneLayout.createParallelGroup()
                        .addGroup(contentPaneLayout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(questionLabel)
                                .addGap(5, 5, 5)
                                .addComponent(questionField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(answerLabel)
                                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(answerField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                        .addComponent(addBtn)
                                        .addComponent(finishBtn)
                                        .addComponent(clearBtn))
                                .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        pack();
        setLocationRelativeTo(getOwner());
    }
    private JTextField questionField;
    private JTextField answerField;
    private JLabel questionLabel;
    private JLabel answerLabel;
    private JButton addBtn;
    private JButton clearBtn;
    private JButton finishBtn;
}
